﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace ContentViewLib.Graphic
{
    public sealed class WidthConverter : IValueConverter
    {

        public object Convert(object o, Type type, object parameter, CultureInfo culture)
        {

            ListView l = o as ListView;

            GridView g = l.View as GridView;

            double total = 0;

            for (int i = 0; i < g.Columns.Count - 1; i++)
            {

                total += g.Columns[i].Width;

            }

            return (l.ActualWidth - total);

        }

        public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
        {

            throw new NotSupportedException();

        }

    }


    public sealed class RowsConvertor : IValueConverter
    {
        public static int outDatedCount;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                ListViewItem item = (ListViewItem)value;
                ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;

                int index = listView.ItemContainerGenerator.IndexFromContainer(item);
                System.Data.DataRowView valueNow = (System.Data.DataRowView)listView.Items[index];
                DateTime datumDo;
                return Coloration(index, valueNow, out datumDo);
            }
            catch
            {
                return Brushes.LightBlue;
            }
        }

        private static object Coloration(int index, System.Data.DataRowView valueNow, out DateTime datumDo)
        {
            if (DateTime.TryParse(valueNow.Row["do"].ToString(), out datumDo))
            {
                if (datumDo.Date < DateTime.Now.Date)
                {
                    outDatedCount++;
                    return new LinearGradientBrush(Colors.LightGray, Colors.Red, new Point(0, -0.5f), new Point(0, 1));
                }
                else if (datumDo.Date == DateTime.Now.Date)
                    return new LinearGradientBrush(Colors.Gray, Colors.Orange, new Point(0, -0.5f), new Point(0, 1));
            }
            if (index % 4 == 0)
                return new LinearGradientBrush(Color.FromArgb(255, 220, 235, 252), Color.FromArgb(255, 193, 220, 252), new Point(0, 0), new Point(0, 1));
            else
                return new LinearGradientBrush(Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 255, 255, 255), new Point(0, 0), new Point(0, 1));
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
